package com.petcare;

import java.util.List;

import com.bd.PetsModule;
import com.bd.Utilities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Pets extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_pets);
	    
	    LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
	    
	    List<PetsModule> list = Utilities.datasource.getAllPets();
	    System.out.println(list.toString());
	    ImageView picture = new ImageView(this);
	    TextView name = new TextView(this);
	    Bitmap bitmap;
	    
	    //LayoutParams lp = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_VERTICAL); 
	    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
	    lp.height = 200;
	    lp.width = 200;
	   
	    picture.setLayoutParams(lp);
	    name.setLayoutParams(lp);
	    
	    
	    
	    for(int i = 0; i < list.size(); i++){
	    	
	    	final PetsModule pet = list.get(i);
	    	name = new TextView(this);
	    	picture = new ImageView(this);
	    	picture.setLayoutParams(lp);
		    name.setLayoutParams(lp);
	    	name.setText(pet.getName());
	    	
	    	if(pet.getPicture().equalsIgnoreCase("")){
	    		picture.setImageResource(R.drawable.ic_launcher);
	    	}
	    	else {
	    		bitmap = BitmapFactory.decodeFile(pet.getPicture());
	    		picture.setImageBitmap(bitmap);
	    	}
	    	
	    	picture.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Bundle bundle = new Bundle();
					bundle.putInt("pk", pet.getPk());
					Intent myIntent = new Intent(Pets.this, profilepet.class);
					myIntent.putExtras(bundle);
	            	startActivity(myIntent);
					
					
				}
			});
	    		    	
	    	//adding view to layout
	    	layout.addView(name);
	    	layout.addView(picture);
	    }
	}
	
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
